# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
import re
from collections import Counter
import xlrd
import xlwt
reload(sys)
sys.setdefaultencoding('utf-8')

QUESTION_RESULT_FILE = 'question_result.txt'
ANSWER_RESULT_FILE = 'answer_result.txt'
DEFAULT_RESULT_FILE = 'default_result.txt'
QUESTION_CSV_FILE = 'question_result.xlsx'
ANSWER_CSV_FILE = 'answer_result.xlsx'

logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"

seperateWordList = [
        '回复：',
        '答复：',
        '【回复】',
        '【主办券商回复】',
        '【主办券商答复】',
        '【公司答复】',
        '【公司回复】',
        '【主办券商及律师回复】',
    ]

reQuestionList = [
        '请主办券商(.*?)回复：',
        '请公司(.*?)回复：',

        '请主办券商(.*?)答复：',
        '请公司(.*?)答复：',

        '请公司(.*?)【回复】',
        '请主办券商(.*?)【回复】',

        '请主办券商(.*?)【主办券商回复】',
        '请主办券商(.*?)【主办券商答复】',

        '请主办券商(.*?)【主办券商及律师回复】',
        '请主办券商(.*?)【主办券商及律师回复】',

        '请公司(.*?)【公司答复】',
        '请公司(.*?)【公司回复】'
        # '[请主办券商|请公司](.*?)[回复：|答复：|【回复】|【答复】|【主办券商回复】|【公司回复】|【主办券商答复】|【公司答复】]'
    ]
reAnswerList = [
        '回复：(.*?)回复：',
        '答复：(.*?)答复：',
        '【回复】(.*?)【回复】',
        '【主办券商回复】(.*?)【主办券商回复】',
        '【答复】(.*?)【答复】',
        '【公司回复】(.*?)【公司回复】'
    ]

def find_question_string_seconddetect(content,seperateWordList):
    cacheString  = ''
    cacheLength = 100000
    for word in seperateWordList:
        if content.find(word)!=-1:
           length = len(content.split(word)[0])
           if length < cacheLength:
               cacheLength = length
               cacheString = content.split(word)[0]
           else:
               continue
        else:
            continue
    return content if cacheString == '' else cacheString


def find_question_string(content,reQuestionList):
    _QuestionList = list()
    for compileItem in reQuestionList:
        for item in re.findall(compileItem,content,re.S):
            item = find_question_string_seconddetect(item,seperateWordList)
            if item!='':
                _QuestionList.append(compileItem.split('.*?')[0] + item)
    return _QuestionList


def find_answer_string(content,reAnswerList,questionStringList):
    _AnswerList = list()
    for compileItem in reAnswerList:
        for item in re.findall(compileItem,content,re.S):
            # for question in questionStringList:
            #     if item.find(question)!=-1:
            #         item = item.replace(question,'')
            if item!='':
                _AnswerList.append(item)
    return _AnswerList


def txt_parse(file,path=''):
    if len(file.split('.'))==2 and file.split('.')[1] == 'txt':
        topString = ''
        with open(path + '/' + file,'r') as f:
            content = f.read()
        lines = open(path + '/' + file).readlines()
        for replaceItem in re.findall('\n\d{1,2}\n',content):
            content = content.replace(replaceItem,'')
        if len(lines)>= 3:
            for line in range(0,3):
                topString += lines[line]
        # if content.find(topString) != -1:
        #     content = content.replace(topString,'')
        if len(re.findall(topString,content))>10:
            content = content.replace(topString,'')

        QuestionList = find_question_string(content,reQuestionList)
        AnswerList = find_answer_string(content,reAnswerList,QuestionList)
        return QuestionList, AnswerList
    else:
        return [],[]


def fankui_anno_txts_parse(filepath):
    files = os.listdir(os.path.abspath(filepath))
    QuestionList = list()
    AnswerList = list()
    defaultList = list()
    count = 1
    for file in files:
        logger.info('No.%d :searching question and answer in file %s'%(count, file))
        questionItem,answerItem = txt_parse(file,path=filepath)
        if len(questionItem) or len(answerItem):
            QuestionList += questionItem
            AnswerList += answerItem
        else:
            defaultList.append(file)
        count += 1
    write_result_file(QuestionList,AnswerList,defaultList)


def write_result_in_csv():
    # csv_question = xlrd.open_workbook('/home/scdev/cc/lawyer-asst/crawler/download_pdf/pdfFile/question_result.xlsx')
    # csv_answer = xlrd.open_workbook('/home/scdev/cc/lawyer-asst/crawler/download_pdf/pdfFile/answer_result.xlsx')
    csv_question = xlwt.Workbook()
    csv_answer = xlwt.Workbook()
    with open('/home/scdev/cc/lawyer-asst/crawler/download_pdf/pdfFile/question_result.txt','r') as f:
        questionString = f.read()
        questionList = questionString.split('-'*200)
        table = csv_question.add_sheet('Sheet1')
        count = 0
        for questionItem in questionList:
            if len(questionItem)<32767:
                table.write(count,0,questionItem.decode('utf-8'))
                count += 1
    # with open('/home/scdev/cc/lawyer-asst/crawler/download_pdf/pdfFile/answer_result.txt','r') as f:
    #     answerString = f.read()
    #     answerList = answerString.split('-'*200)
    #     # table = csv_answer.sheet_by_name('Sheet1')
    #     table = csv_answer.add_sheet('Sheet1')
    #     count = 0
    #     for answerItem in answerList:
    #         table.write(count,0,answerItem.decode('utf-8'))
    #         count += 1
    csv_question.save('question_result.xlsx')
    # csv_answer.save('answer_result.xlsx')

def write_result_file(QuestionList,AnswerList,defaultList):
    with open(QUESTION_RESULT_FILE,'w') as Q:
        for questionResult in QuestionList:
            Q.write(questionResult)
            Q.write('\n')
            Q.write('-' * 200)
            Q.write('\n')
    with open(ANSWER_RESULT_FILE,'w') as A:
        for answerResult in AnswerList:
            A.write(answerResult)
            A.write('\n')
            A.write('-' * 200)
            A.write('\n')
    with open(DEFAULT_RESULT_FILE,'w') as D:
        for defaultItem in defaultList:
            D.write(defaultItem)
            D.write('\n')

if __name__ == '__main__':
    write_result_in_csv()
    # fankui_anno_txts_parse('/home/scdev/cc/lawyer-asst/crawler/download_pdf/pdfFile/pdf /txt')
    # files = os.listdir(os.path.abspath(''))
    # # import codecs
    # QuestionList = list()
    # AnswerList = list()
    # defaultList = list()
    # # resultStringQuestion = list()
    # for file in files:
    #     questionItem,answerItem = txt_parse(file)
    #     if len(questionItem) or len(answerItem):
    #         QuestionList += questionItem
    #         AnswerList += answerItem
    #     else:
    #         defaultList.append(file)
    #
    #
    #
    # for result in QuestionList:
    #     print result
    #     print '-' * 200
    # print '*'*200
    # for result in AnswerList:
    #     print result
    #     print '-' * 200
    # file = '1.txt'
    # with open(file, 'r') as f:
    #     content = f.read()
    # print re.search('1、(.*?)答复：',content,re.S).group()

            # linesInfoDict = Counter(content.split('\n'))
            # for linesInfo,count in linesInfoDict.items():
            #     if count>=20 and linesInfo!='':
            #         print linesInfo
            #         content = content.replace(linesInfoDict,'')


            # for item in re.findall('[\s^]\d[、\.]+(?!\d)(.*?)回复|答复：',content,re.S):
            #     print item + '\n\n'
            #     print '-' * 200

